package com.cbgolf.oa.activity.publiz;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import com.cbgolf.oa.R;
import com.cbgolf.oa.base.BaseActivity;
import com.cbgolf.oa.entity.ParkBean;
import com.cbgolf.oa.manager.ReceiverManager;
import com.cbgolf.oa.model.LoginModel;
import com.cbgolf.oa.presenter.ILoginPresenter;
import com.cbgolf.oa.service.LocationService;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.ScreenUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.views.LoginView;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Created by seiya on 2016/11/8.
 */
public class LoginActivity extends BaseActivity implements ILoginPresenter {

  private LoginModel model;
  private LoginView view;
  private Handler handler = new Handler();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a_login);
  }

  @Override
  protected void onKeyBack() {
    moveTaskToBack(true);
  }

  @Override
  protected void findViews() {
    super.findViews();
    view = new LoginView(this, this);
    model = new LoginModel(this);
  }

  @Override
  protected void onResume() {
    super.onResume();
    LocationService.stopLocation();
    DataUtil.quitUser();
    ReceiverManager.getInstance().unBindAliAccount();
    ReceiverManager.getInstance().closeWebSocket();
  }

  @Override
  public void checkLoginInfo(String account, String pwd, String name, String startName) {

  }

  @Override
  public void doLogin(String account, String pwd) {

  }

  @Override
  public void subMit(String account, String pwd, String parkName, String startName) {
    model.checkLoginInfo(account, pwd, parkName, startName);
  }

  @Override
  public void searchPark(final String name) {
    handler.postDelayed(() -> model.searchPark(name), 300);
  }

  @Override
  public void showProgress() {
    AnimaUtil.showLoading(context, false);

  }

  @Override
  public void stopProgress() {
    AnimaUtil.stopLoading();
  }

  @Override
  public void getDataFail(String msg) {

  }

  @Override
  public void getDataSuccess(Object obj) {

  }

  @Override
  public void loginSuccess() {
    try {
      showLoginSuccessDialog();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public void onError(String errorMsg) {
    if (Util.isNull(errorMsg)) {
      Util.showNetError();
      return;
    }
    Util.show(TextUtil.contains(errorMsg, "非法") ? "账号或密码错误" : errorMsg);
  }

  @Override
  public void getParkSuccess(List<ParkBean> obj, String name) {
    view.getParkSuccess(obj, name);
  }

  private Dialog successDialog;

  public void showLoginSuccessDialog() {
    if (isFinishing()) {
      return;
    }
    if (null != successDialog && successDialog.isShowing()) {
      return;
    }
    successDialog = new Dialog(context, R.style.mDialog);
    @SuppressLint("InflateParams") View view = getLayoutInflater()
        .inflate(R.layout.dialog_messge, null);
    View layout = view.findViewById(R.id.dialog_msg_layout_ll);
    LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) layout.getLayoutParams();
    ll.height = LayoutParams.WRAP_CONTENT;
    ll.width = ScreenUtil.getWidth() * 4 / 5;
    layout.setLayoutParams(ll);
    AutoUtil.auto(view);
    successDialog.setContentView(view);
    successDialog.setCancelable(false);
    if (!isFinishing()) {
      successDialog.show();
      Observable.timer(1500, TimeUnit.MILLISECONDS)
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe(aLong -> {
            successDialog.dismiss();
            mStartActivity(MainActivity.class);
          });
    }
  }

  @Override
  public String getActivityTitle() {
    return null;
  }

  @Override
  public void back() {
    finish();
  }

  @Override
  public void getDataFail(String msg, int code) {

  }
}
